package com.lx.forum.controller;

import com.lx.forum.common.AppResult;
import com.lx.forum.common.ResultCode;
import com.lx.forum.exception.ApplicationException;
import com.lx.forum.model.Board;
import com.lx.forum.services.IBoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 李夏
 * @version 1.0
 */
@RestController
@Slf4j
@Tag(name = "板块测试接口", description = "版块测试接口")
@RequestMapping("/board")
public class BoardController {

    @Value("${lx-forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Autowired
    private IBoardService boardService;

    @GetMapping("/topList")
    @Operation(summary = "获取首页板块列表")
    public AppResult<List<Board>> topList(){
        log.info("首页版块个数为：" + indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if(boards==null){
            boards=new ArrayList<>();
        }
        return AppResult.success(boards);

    }
    @Operation(summary = "获取版块信息")
    @GetMapping("getById")
    public AppResult<Board> getById (@Parameter(description="板块id" )@RequestParam(name = "id", required = false) Long id) {
        // 调用Service
        Board board = boardService.selectById(id);
        // 对查询结果进行校验
        if (board == null || board.getDeleteState() == 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        // 返回结果
        return AppResult.success(board);
    }


}
